package com.orangelife.mobile.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.jump.CommonJumper;
import com.common.sp.UserSp;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.orangelife.mobile.main.biz.JumpUtil;
import com.orangelife.mobile.util.BaseViewHolder;
import com.orangelife.mobile.util.ToastHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Administrator on 2015/11/13.
 */
public class FeatruesAdapter extends BaseAdapter {

    private Context mContext;
    private List<Map<String, Object>> list;
    private ImageDownloader image = ImageDownloader.getImageDownloader();
    private int type;


    public FeatruesAdapter(Context context, List<Map<String, Object>> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_features, null);
        }
        Map<String, Object> map = list.get(position);
        TextView tv = BaseViewHolder.get(convertView, R.id.text);
        ImageView iv = BaseViewHolder.get(convertView, R.id.image);
        LinearLayout ll = BaseViewHolder.get(convertView, R.id.llfeature);
        image.download(map.get("icon").toString(), iv,
                ImageDownloader.ImageSize.IMAGE_SIZE_ORIGINAL);
        tv.setText(map.get("modeName").toString());
        ll.setOnClickListener(new Listener(map, position));
        return convertView;
    }

    public BaseAdapter setList(List<Map<String, Object>> list) {
        this.list = list;
        return FeatruesAdapter.this;
    }

    class Listener implements View.OnClickListener {

        Map<String, Object> map = new HashMap<String, Object>();
        int position;

        public Listener(Map<String, Object> map, int position) {
            this.map = map;
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.llfeature:
                    //数据采集体不要删除
                    Map<String,Object> trackmap=new HashMap<>();
                    trackmap.put("UI_ELMT",String.valueOf(map.get("modeName")));
                    trackmap.put("UI_PATH","/houseKpr");

                    //点击进入商品分类页
                    if (map.get("isopen").equals("1")) {
                        if (map.get("iscert").equals("1")) {//需要认证
                            if (!StringUtil.isContainBlank(UserSp.getLoginID())) {
                                JumpUtil.jumpActivity(mContext, String.valueOf(map.get("uri")),trackmap);
                            } else {
                                CommonJumper.jumpToLogin(mContext);
                            }
                        } else {
                            //跳转功能
                            JumpUtil.jumpActivity(mContext, String.valueOf(map.get("uri")),trackmap);
                        }
                    } else {
                        ToastHelper.getInstance()._toast(map.get("openInfo") + "");
                    }
                    break;
                default:
                    break;
            }
        }
    }


}
